﻿namespace Sidvall.Net
{
    public class LanguageHeaderCollection : System.Collections.ObjectModel.Collection<LanguageHeader>
    {
        #region Public Members

        #region Add

        public void Add(string cultureName)
        {
            Add(cultureName, null);
        }
        public void Add(string cultureName, double? quality)
        {
            var item = new LanguageHeader()
            {
                CultureName = cultureName,
                Quality = quality,
            };
            Add(item);
        }

        #endregion
        #region Copy

        public LanguageHeaderCollection Copy()
        {
            var items = new LanguageHeaderCollection();
            foreach (var languageHeader in this)
            {
                var item = languageHeader.Copy();
                items.Add(item);
            }
            return items;
        }

        #endregion
        #region IsEqual

        public bool IsEqual(LanguageHeaderCollection obj)
        {
            return IsEqual(this, obj);
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            var sb = new System.Text.StringBuilder();
            foreach (var item in this)
            {
                var value = item.ToString();
                if (string.IsNullOrWhiteSpace(value))
                    continue;
                if (sb.Length > 0)
                    sb.Append(", ");
                sb.Append(value);
            }
            return "Accept-Language: " + sb.ToString();
        }

        #endregion

        #region IsEqual - Static

        public static bool IsEqual(LanguageHeaderCollection item1, LanguageHeaderCollection item2)
        {
            if (item1 == null)
            {
                if (item2 == null)
                    return true;
                else
                    return false;
            }
            else if (item2 == null)
                return false;
            else if (item1.Count != item2.Count)
                return false;
            for (int i = 0; i < item1.Count; i++)
            {
                if (!item1[1].IsEqual(item2[2]))
                    return false;
            }
            return true;
        }

        #endregion

        #endregion
    }
}
